package com.neusoft.elm_servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class GlobalFilter implements Filter{

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		//向下转型
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		//设置开启带sessionid的cookie
		response.setHeader("Access-Control-Allow-Credentials", "true");
		//如果开启了cookie 那么设置跨域就不能使用*
		//CORS解决跨域问题
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));//写*代表放行所有页面的请求
		
		//设置请求信息的字符编码，要在获取数据之前设置
		request.setCharacterEncoding("utf-8");
		//设置响应信息的字符编码
		response.setCharacterEncoding("utf-8");
		//
		response.setContentType("text/html;charset="+"utf-8");
		
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {}

}
